﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using CustomControls.InheritedFrameworkControls;
using CustomControls.Maps;
using CustomControls.MiscControls;
using CustomHelpers.Extensions;
using Infrasturcture.Global.Controls.LobbyMap;
using Infrasturcture.TouchLibrary;
using MallInteractiveMap.ClientDataHandler;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;
using TouchFramework;

namespace MallInteractiveMap
{
    public partial class MainWindow
    {
        private void LoadFloorList()
        {
            foreach (var floor in Floors)
            {
                var floorButton = new TouchRadioButton
                {
                    Style = (Style)FindResource("FloorToggleButtonStyle"),
                    Tag = floor,
                    Margin = new Thickness(0, 2, 0, 2),
                    Content = floor.Name,
                    GroupName = "Floors"
                };
                floorButton.Checked += FloorButtonChecked;
                FloorContainer.Children.Add(floorButton);
                FrameworkManager.RegisterElement((IMTouchControl)floorButton, false, new[] { TouchAction.Tap });
            }
        }

        private void ClearFloorList()
        {
            if (FloorContainer.Children.Count <= 0) return;
            foreach (var floorButton in FloorContainer.Children)
            {
                FrameworkManager.UnRegisterElement((IMTouchControl)floorButton);
                ((TouchRadioButton)floorButton).Checked -= FloorButtonChecked;
            }
            FloorContainer.Children.Clear();
        }

        void FloorButtonChecked(object sender, RoutedEventArgs e)
        {
            var floor = ((RadioButton)sender).Tag as DTOFloor;
            CurrentFloor = floor;
            CurrentFloorTitle = floor.Name;
        }


        private static void OnCurrentFloorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var window = d as MainWindow;
            if (e.NewValue != null)
                window.LoadFloor(e.NewValue as DTOFloor);
        }

        private void LoadFloor(DTOFloor floor)
        {
            ClearMaps(MapHolder, FrameworkManager);
            if (floor != null)
            {
                LoadMap(floor.FloorIndex, floor.Id);
            }
        }

        private static void ClearMaps(Canvas mapHolder, FrameworkManger frameworkManager)
        {
            var maps = mapHolder.FindChildren<AMap>();
            foreach (var amap in maps.ToList())
            {
                var map = (IMap)amap;
                foreach (MapElement mapElement in map.MapElements.ToList())
                {
                    frameworkManager.UnRegisterElement(mapElement.Container.Id);
                }
                frameworkManager.UnRegisterElement((IMTouchControl)map);
                mapHolder.Children.Remove((UIElement)map);
            }
        }

        void MapElementActivated(object sender, EventArgs e)
        {
            var mapElement = sender as MapElement;
            WaitScreen.Show();
            var shop = GetShop(mapElement.Id);
            if (shop != null)
            {
                ShowShopInformation(shop, mapElement);
            }
            else
            {
                mapElement.DeActivate();
                ShowMessage("", LanguageLoader.GetText("The Shop has no details available"));
            }
            WaitScreen.Hide();
        }
        

        private void LoadMap(int floorIndex, int floorId)
        {
            WaitScreen.Show();
            var lobbyMap = MapPresenter.GetMap(floorIndex);
            lobbyMap.Width = MapHolder.ActualWidth;
            lobbyMap.Height = MapHolder.ActualHeight;
            Canvas.SetLeft((UIElement)lobbyMap, 0);
            Canvas.SetTop((UIElement)lobbyMap, 0);
            MapHolder.Children.Add((UIElement)lobbyMap);
            foreach (var mapElement in lobbyMap.MapElements)
            {
                var element = (MapElement)mapElement;
                element.Id = element.Viewbox.Tag.ToString();
                element.Viewbox.Tag = element;
                element.Container = FrameworkManager.RegisterElement(element.Viewbox, false, new[] { TouchAction.Tap });
                element.Activated += MapElementActivated;
                //if (ConfigurationManager.AppSettings["SaveDemoShops"].Equals("true"))
                //    SaveShops(element.Id, floorId);

            }
            WaitScreen.Hide();
        }

       

      

        private void CheckFloorButton(DTOFloor floor)
        {
            foreach (var child in FloorContainer.Children)
            {
                var touchRadioButton = child as TouchRadioButton;
                var floorOption = touchRadioButton.Tag as DTOFloor;
                if (floorOption.Id == floor.Id)
                {
                    touchRadioButton.IsChecked = true;
                    break;
                }
            }
        }
    }
}
